package com.westar.base.businessUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.westar.core.conf.SysConst;
import com.westar.base.exceptions.LoginException;
import com.westar.base.exceptions.SelfHttpException;
import com.westar.base.model.Implement;
import com.westar.base.model.ImplementDetail;
import com.westar.base.model.UserDepAreaRelate;
import com.westar.base.pojo.PageBean;
import com.westar.base.util.WestarHttpUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserPageUtil {


    /**
     * 取得所有区域数据信息
     * @param actCookie
     * @return
     * @throws IOException
     * @throws LoginException
     */
    public static List<Implement> queryAllImplement(String actCookie) throws IOException, SelfHttpException {
        List<Implement> implementList = new ArrayList<>();

        String url = "http://"+ SysConst.DOMAIN+"/directoryLevel/queryTreeImplement";

        Map<String,String> searchForm = new HashMap<>( );

        String responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
        JSONArray rows = JSONArray.parseArray( responeBody);
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject implementJson = JSONObject.parseObject(data.toString());

                Implement implement = new Implement();
                implement.setImplementId(implementJson.getString( "id" )  );
                implement.setName(implementJson.getString( "name" ));
                implement.setParentId(implementJson.getInteger( "parentId"));
                implement.setCode(implementJson.getString( "code"));

                implementList.add(implement);

                String state = implementJson.getString( "state");
                if(state.equals( "closed" )){
                    List<Implement> areaSubList = UserPageUtil.getImplementFromPage(url,actCookie,implement.getImplementId());
                    implementList.addAll(areaSubList);
                }
            }
        }

        return implementList;
    }


    private static List<Implement> getImplementFromPage(String url, String actCookie, String parentId)
            throws IOException, SelfHttpException {
        List<Implement> implementList = new ArrayList<>();
        Map<String,String> searchForm = new HashMap<>( );
        searchForm.put( "id",parentId);

        String responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
        JSONArray rows = JSONArray.parseArray( responeBody);
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject implementJson = JSONObject.parseObject(data.toString());

                Implement implement = new Implement();
                implement.setImplementId(implementJson.getString( "id" )  );
                implement.setName(implementJson.getString( "name" ));
                implement.setParentId(implementJson.getInteger( "parentId"));
                implement.setCode(implementJson.getString( "code"));

                implementList.add(implement);

                String state = implementJson.getString( "state");
                if(state.equals( "closed" )){
                    List<Implement> areaSubList = UserPageUtil.getImplementFromPage(url,actCookie,implement.getImplementId());
                    implementList.addAll(areaSubList);
                }

            }
        }
        return implementList;
    }

    /**
     * 获取事项详情
     * @param actCookie
     * @return
     */
    public static PageBean<ImplementDetail> queryAllImplementDetail(String actCookie,Integer page,Integer rows)
            throws IOException, SelfHttpException {

        PageBean<ImplementDetail> implementList = new PageBean<ImplementDetail>();

        String url = "http://"+ SysConst.DOMAIN+"/implementList/queryList";

        Map<String,String> searchForm = new HashMap<>( );
        searchForm.put( "page",page.toString() );
        searchForm.put( "rows",rows.toString() );

        String responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
        JSONObject res = JSONObject.parseObject(responeBody);
        Integer total = res.getInteger("total");
        implementList.setTotalCount(total);
        if(total > 0){
            List<ImplementDetail> recordList = UserPageUtil.getImplementDetailPage(res);
            implementList.setRecordList(recordList);
        }
        return implementList;

    }

    private static List<ImplementDetail> getImplementDetailPage(JSONObject res) {
        List<ImplementDetail> areaList = new ArrayList<>();
        JSONArray rows = JSONArray.parseArray( res.getString( "rows" ));
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject areaJson = JSONObject.parseObject(data.toString());
                ImplementDetail implementDetail = new ImplementDetail();

                implementDetail.setImplListId(areaJson.getString( "idForStr" )  );

                String appoveObjects = areaJson.getString( "appoveObject" );
                StringBuilder appoveObjectBuilder = new StringBuilder();
                String appoveObject = appoveObjectBuilder.toString();

                if(StringUtils.isNotEmpty(appoveObjects)){
                    char[] appoveObjectChar = appoveObjects.toCharArray();
                    for( char c: appoveObjectChar ){
                        appoveObjectBuilder.append(",");
                        appoveObjectBuilder.append(c);
                    }
                    appoveObject = appoveObjectBuilder.toString().substring( 1 );
                }

                implementDetail.setAppoveObject(appoveObject);
                implementDetail.setEventName(areaJson.getString( "eventName"));
                implementDetail.setPromiseTimeLimit(areaJson.getString( "promiseTimeLimit"));
                implementDetail.setLegalTimeLimit(areaJson.getString( "legalTimeLimit"));
                implementDetail.setMajorTerm(areaJson.getString( "majorTerm"));
                implementDetail.setDeptId(areaJson.getString( "deptId"));
                implementDetail.setAreaCode(areaJson.getString( "areaCode"));
                implementDetail.setShowEventCode(areaJson.getString( "showEventCode"));
                implementDetail.setUpdateTime(areaJson.getString( "updateTime"));
                areaList.add(implementDetail);
            }
        }
        return areaList;
    }

    /**
     *
     * @param actCookie
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    public static PageBean<UserDepAreaRelate> listPagedUserFromUp(
            String actCookie,Integer page,Integer rows,String loginIdName) throws IOException {

        PageBean<UserDepAreaRelate> userDepAreaRelatePageBean = new PageBean<UserDepAreaRelate>();

        String url = "http://"+ SysConst.DOMAIN+"/oauthui/user/listData";

        Map<String,String> searchForm = new HashMap<>( );
        if(StringUtils.isNotEmpty(loginIdName)){
            searchForm.put( "loginIdName",loginIdName);
        }
        searchForm.put( "page",page.toString() );
        searchForm.put( "rows",rows.toString() );
        searchForm.put( "_",new Date().getTime()+"" );

        String responeBody = WestarHttpUtil.getMethod( url,actCookie,searchForm);
        JSONObject res = JSONObject.parseObject(responeBody);
        Integer total = res.getInteger("total");
        userDepAreaRelatePageBean.setTotalCount(total);
        if(total > 0){
            List<UserDepAreaRelate> recordList = UserPageUtil.getUserInfoPage(res);
            userDepAreaRelatePageBean.setRecordList(recordList);
        }
        return userDepAreaRelatePageBean;

    }

    /**
     * 获取分页数据的
     * @param res
     * @return
     */
    private static List<UserDepAreaRelate> getUserInfoPage(JSONObject res) {
        List<UserDepAreaRelate> userDepAreaRelates = new ArrayList<>();
        JSONArray rows = JSONArray.parseArray( res.getString( "rows" ));
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject areaJson = JSONObject.parseObject(data.toString());
                UserDepAreaRelate userDepAreaRelate = new UserDepAreaRelate();

                userDepAreaRelate.setName(areaJson.getString( "name" ));
                userDepAreaRelate.setAccount(areaJson.getString( "loginId" ));
                userDepAreaRelate.setDeptId(areaJson.getString( "deptId" ));
                userDepAreaRelate.setAreaId(areaJson.getString( "areaId"));
                userDepAreaRelate.setMd5Passwd(areaJson.getString( "password"));
                userDepAreaRelate.setUpdateTime(areaJson.getString( "updateTime"));
                userDepAreaRelates.add(userDepAreaRelate);
            }
        }
        return userDepAreaRelates;
    }
}
